// 不做拦截的接口
let urls = ['/admin/register', '/admin/login']
module.exports = (options, app) => {
  return async function verifyToken(ctx, next) {
    if (/^\/admin\//.test(ctx.request.url)) { // 后台验证
      await verifyTokenAdim(ctx, next);
    } else {
      await next();
    }
  }
}
async function verifyTokenAdim(ctx, next) {
  if (urls.indexOf(ctx.request.url) !== -1) {
    await next();
    return
  }
  let token = ctx.helper.cookieGet('token');
  // console.log('验证-------------------------------------',token)
  if (!token) {
    ctx.body = {
      code: 10301,
      msg: '你还未登录！'
    }
    return
  }
  let {
    state,
    data
  } = await ctx.helper.verifyToken(token)
  if (state == 2) {
    ctx.body = {
      msg: '登录超时，请重新登录！',
      code: 10301
    }
  } else if (state == 3) {
    ctx.body = {
      msg: '你的账号在其他地方登录，请重新登录！',
      code: 10302
    }
  } else if (state == 4) {
    ctx.body = {
      msg: 'token验证失败！',
      code: 10400
    }
  } else {
    // console.log(data,'92834928349283498')
    //  ctx.request.body.userId = 67;
    ctx.request.body.userId = data.userId;
    ctx.request.body.tokenRoleType = data.roleType;
    await next();
  }
}